#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <QTcpSocket>
#include <QHostAddress>
#include <QThread>
#include "video.h"

namespace Ui {
class MainWindow;
}

union Int2Byte
{
    int intData;
    char byteData[4];
};
class CarControl//案件数量过多则使用QMap
{
public:
    CarControl(){ reSet(); }
    ~CarControl(){ reSet(); }

    int getState_WS(){ return state_WS; }
    int getState_DA(){ return state_DA; }
    int getServo_X(){ return servo_X; }
    int getServo_Y(){ return servo_Y; }

    void setTcp(QTcpSocket *tcp)
    {
        tcpSend = tcp;
    }
    void setControlFlag(bool flag){ startControl = flag; }
    void setSensitive(uchar x,uchar y)
    {
        sensitiveX = x;
        sensitiveY = y;
        qDebug()<<"sensitiveX:"<<sensitiveX<<"   sensitiveY:"<<sensitiveY;
    }
    void setPress(Qt::Key key)
    {
        switch (key) {
        case Qt::Key_W: key_W = 1; break;
        case Qt::Key_S: key_S = 1; break;
        case Qt::Key_D: key_D = 1; break;
        case Qt::Key_A: key_A = 1; break;
        case Qt::Key_Shift: key_Shift = 1; break;
        case Qt::Key_Space: key_Space = 1; break;
        default: break;
        }
        calculateState();
    }
    void setRelase(Qt::Key key)
    {
        switch (key) {
        case Qt::Key_W: key_W = 0; break;
        case Qt::Key_S: key_S = 0; break;
        case Qt::Key_D: key_D = 0; break;
        case Qt::Key_A: key_A = 0; break;
        case Qt::Key_Shift: key_Shift = 0; break;
        case Qt::Key_Space: key_Space = 0; break;
        default: break;
        }
        calculateState();
    }
    void servoPosChanged(int dx,int dy)
    {
        servo_X += dx * sensitiveX / 10;
        servo_Y += dy * sensitiveY / 10;
        calculateState();
    }

private:
    QTcpSocket *tcpSend;
    bool key_W,key_S,key_A,key_D;
    int state_WS,state_DA;
    int servo_X,servo_Y;
    bool key_Shift;
    bool key_Space;
    uchar sensitiveX,sensitiveY;
    bool startControl;

    void reSet()
    {
        key_W = 0; key_S = 0; key_A = 0; key_D = 0;
        state_WS = 0;   state_DA = 0;
        servo_X = 0;    servo_Y = 0;
        key_Shift = 0;  key_Space = 0;
        sensitiveX = 5; sensitiveY = 5;
        startControl = 0;
    }
    void calculateState()
    {
        if(!startControl) return;
        state_WS = (key_W - key_S) * (600 + key_Shift * 400);       //计算前后动力
        state_DA = (key_D - key_A) * 1000;                          //计算左右角度
        if(key_Space)       //计算舵机角度
        {
            servo_X = 0;
            servo_Y = 0;
        }
        if(servo_X > 1000)  servo_X = 1000;
        if(servo_X < -1000) servo_X = -1000;
        if(servo_Y > 1000)  servo_Y = 1000;
        if(servo_Y < -1000) servo_Y = -1000;

        QByteArray data = makeFrame(state_WS,state_DA,servo_X,servo_Y);
        tcpSend->write(data);

        qDebug()<<"state_WS:"<<state_WS<<"   state_DA:"<<state_DA<<"   servo_X:"<<servo_X<<"   servo_Y:"<<servo_Y;
    }

    QByteArray makeFrame(int ws,int da,int sx,int sy)
    {
        QByteArray sendData;

        sendData.append(0xfa);    //表头1
        sendData.append(0xfb);    //表头2
        sendData.append(0x01);  //设备号

        Int2Byte databuff;
        databuff.intData = ws;//前后控制量
        sendData.append(databuff.byteData[0]);
        sendData.append(databuff.byteData[1]);
        sendData.append(databuff.byteData[2]);
        sendData.append(databuff.byteData[3]);

        databuff.intData = da;  //左右控制量
        sendData.append(databuff.byteData[0]);
        sendData.append(databuff.byteData[1]);
        sendData.append(databuff.byteData[2]);
        sendData.append(databuff.byteData[3]);

        databuff.intData = sx;  //舵机X轴控制量
        sendData.append(databuff.byteData[0]);
        sendData.append(databuff.byteData[1]);
        sendData.append(databuff.byteData[2]);
        sendData.append(databuff.byteData[3]);

        databuff.intData = sy;  //舵机Y轴控制量
        sendData.append(databuff.byteData[0]);
        sendData.append(databuff.byteData[1]);
        sendData.append(databuff.byteData[2]);
        sendData.append(databuff.byteData[3]);
        return sendData;
    }
};



class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);

    void keyPressEvent(QKeyEvent *event);
    void keyReleaseEvent(QKeyEvent *event);

private slots:
    void on_pushButton_connectTcp_clicked();
    void onTcpReadData();
    void onTcpError(QAbstractSocket::SocketError);
    void on_pushButton_clicked();
    void on_spinBox_valueChanged(int arg1);
    void on_mouseMove(QPoint pos);
    void on_spinBox_x_valueChanged(int arg1);
    void on_spinBox_y_valueChanged(int arg1);
    void on_pushButton_quit_clicked();
    void on_pushButton_max_clicked();
    void on_pushButton_min_clicked();
    void on_pushButton_about_clicked();

private:
    Ui::MainWindow *ui;
    QTcpSocket *myTcp;
    Video *myVedio;
    QThread *videoThread;
    bool flagVideo;
    CarControl carState;
    qint64 tcpSend(QByteArray data);
    qint64 tcpSend(QString data);
};

#endif // MAINWINDOW_H


